﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LogicalComponent.userInterface;
using LogicalComponent.util;
using LogicalComponent.util.exception;

namespace TestLogicalComponent.userInterface
{
    [TestClass]
    public class TestClickUtil
    {
        [TestMethod]
        public void TestGetClickCoordinatesSquareTiles()
        {
            Tuple<int, int, int, Coordinate>[] singles = {
                               new Tuple<int, int, int, Coordinate>
                                   (0, 0, 1, new Coordinate(0, 0)),
                               new Tuple<int, int, int, Coordinate>
                                   (0, 0, 5, new Coordinate(0, 0)),
                               new Tuple<int, int, int, Coordinate>
                                   (7, 15, 14, new Coordinate(0, 1)),
                                                         }; 
            foreach (Tuple<int, int, int, Coordinate> tuple in singles)
            {
                ClickUtil.ClickMap map = new ClickUtil.ClickMap(0, 0, tuple.Item3);
                Coordinate expected = tuple.Item4;
                Coordinate actual = ClickUtil.GetClickCoordinate(tuple.Item1, tuple.Item2, map);
                Assert.IsTrue(expected == actual);          
            }
        }

        [TestMethod]
        public void TestGetClickCoordinateRectangularTiles()
        {
            Tuple<int, int, int, int, Coordinate>[] pairs = {
                               new Tuple<int, int, int, int, Coordinate>
                                   (0, 0, 1, 1, new Coordinate(0, 0)),
                               new Tuple<int, int, int, int, Coordinate>
                                   (3, 10, 1, 15, new Coordinate(3, 0)),
                               new Tuple<int, int, int, int, Coordinate>
                                   (55, 20, 10, 20, new Coordinate(5, 1)),
                                                            };
            foreach (Tuple<int, int, int, int, Coordinate> tuple in pairs)
            {
                ClickUtil.ClickMap map = new ClickUtil.ClickMap(0, 0, tuple.Item3, tuple.Item4);
                Coordinate expected = tuple.Item5;
                Coordinate actual = ClickUtil.GetClickCoordinate(tuple.Item1, tuple.Item2, map);
                Assert.IsTrue(expected == actual);
            }
        }

        [TestMethod]
        public void TestGetClickCoordinateErrors()
        {
            Tuple<int, int, int, int>[] errors = {
                               new Tuple<int, int, int, int>(2, 2, 0, 1),
                               new Tuple<int, int, int, int>(2, 2, 1, 0),
                               new Tuple<int, int, int, int>(2, 2, -1, 1),
                               new Tuple<int, int, int, int>(2, 2, 1, -1),
                                                 };
            foreach (Tuple<int, int, int, int> tuple in errors)
            {
                try
                {
                    ClickUtil.ClickMap map = new ClickUtil.ClickMap(0, 0, tuple.Item4, tuple.Item3);
                    ClickUtil.GetClickCoordinate(tuple.Item1, tuple.Item2, map);
                    Assert.Fail();
                }
                catch (LogicalUserError) { }
            }
        }

        [TestMethod]
        public void TestGetClickCoordinateOffOrigin()
        {
            Tuple<float, float, float, float, float, Coordinate>[] singles = {
                               new Tuple<float, float, float, float, float, Coordinate>
                                   (0, 0, 1, 15.5f, -15.5f, new Coordinate(-16, 15)),
                               new Tuple<float, float, float, float, float, Coordinate>
                                   (0, 0, 5, 16, -16, new Coordinate(-4, 3)),
                               new Tuple<float, float, float, float, float, Coordinate>
                                   (7, 15, 14, -3, 10, new Coordinate(0, 0)),
                               new Tuple<float, float, float, float, float, Coordinate>
                                   (44.58f, 12.72f, 15, -5.23f, -4.77f, new Coordinate(3, 1))
                                                         };
            foreach (Tuple<float, float, float, float, float, Coordinate> tuple in singles)
            {
                ClickUtil.ClickMap map = new ClickUtil.ClickMap(tuple.Item4, tuple.Item5, tuple.Item3);
                Coordinate expected = tuple.Item6;
                Coordinate actual = ClickUtil.GetClickCoordinate(tuple.Item1, tuple.Item2, map);
                Assert.IsTrue(expected == actual, String.Format("Test with origin ({0}, {1}), size {2}, " +
                                                                "and click ({3}, {4}) gave gave coordinate {5}, " +
                                                                "not expected coordinate {6}", 
                                                                tuple.Item4, tuple.Item5, tuple.Item3, tuple.Item1, 
                                                                tuple.Item2, actual, expected));
            }
        }
    }
}
